package dormouse.fx.metadata;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;

import org.apache.commons.vfs2.FileObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dormouse.fx.renderers.ImageRenderer;
import dormouse.vfs.event.IFileSelectionEvent;
import dormouse.vfs.metadata.IMetadataViewer;
import dormouse.vfs.ui.renderers.FileRendererFactory;
import dormouse.vfs.ui.renderers.IFileRenderer;

public class DefaultMetadataRenderer implements IMetadataViewer<Node>, Initializable {
	
	/** Class logger.*/
	private static final Logger log = LoggerFactory
			.getLogger(DefaultMetadataRenderer.class);
	
	private AnchorPane component;
	
	private static final String[] EXTS = new String[]{IMetadataViewer.DEFAULT};
	
    @FXML //  fx:id="previewImage"
    private Node previewImage; // Value injected by FXMLLoader

    @FXML //  fx:id="propertyTable"
    private TableView<?> propertyTable; // Value injected by FXMLLoader

	private PropertyPanelController controller;

	
	public DefaultMetadataRenderer(){
		
	}

	@Override
	public void preview(FileObject file) {
		IFileRenderer<Node> preview = getPreviewComponent(file);
		if (preview != null){
			preview.render(file);
			previewImage = preview.getComponent();
		}
		
	}

	@Override
	public IFileRenderer<Node> getPreviewComponent(FileObject file) {
		IFileRenderer<Node> previewComponent =  FileRendererFactory.getInstance(file);
		if (previewComponent == null){
			previewComponent = new ImageRenderer();
		}
		return previewComponent;
	}

	@Override
	public Node getComponent() {
		return component;
	}

	
	private void init() {
	 	URL url = getClass().getResource("/PDFMetadataPanel.fxml");
        FXMLLoader loader = new FXMLLoader(url);

        try {
            component = (AnchorPane) loader.load();
            controller = (PropertyPanelController) loader.getController();            

        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
		
	}

	@Override
	public void fileSelected(IFileSelectionEvent event) {
		preview(event.getSelectedFile());
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] getExtensions() {
		return EXTS;
	}

}
